package net.comm.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.comm.util.Function;
import net.comm.util.StringUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
public abstract class BaseServlet extends HttpServlet {
 
 
    private static final long serialVersionUID = 520131401655L;
 
    protected Log log = LogFactory.getLog(this.getClass());
 
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
       execute(request, response);
    }
 
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
       execute(request, response);
    }
 
    /**
     *由子类实现,处理GET和POST请求
     *
     *@paramrequest
     *@paramresponse
     *@throwsServletException
     *@throwsIOException
     */
    public abstract void execute(HttpServletRequest request,
           HttpServletResponse response) throws ServletException, IOException;
 
    protected final String getValue(HttpServletRequest request, String param) {
       String str = request.getParameter(param);
       if (str == null)
           return "";
       return str;
    }
 
    private static byte[] lock = new byte[0];
 
    protected final void setNoCache(HttpServletRequest request,
           HttpServletResponse response) {
       synchronized (lock) {
           if (request.getProtocol().compareTo("HTTP/1.0") == 0)
              response.setHeader("Prama", "no-cache");
           else if (request.getProtocol().compareTo("HTTP/1.1") == 0)
              response.setHeader("Cache-Control", "no-cache");
           response.setDateHeader("Expires", 0);
       }
    }
	protected void doInfo(String msginfo, PrintWriter out) throws UnsupportedEncodingException{

		
		System.out.println(msginfo);

		String[] js1 = new String[1];

		js1[0] = "/js/common.js";

		out.println(Function.getHtmlHead("错误信息",	"/images/printimg.gif", js1).toString());

		out.println("<script>");
		out.print("alert('");
		out.print(StringUtil.cnCodeTrans(msginfo));
		out.println("');");
		out.println("history.back();");
		out.println("</script>");
		out.println("</body>");
		out.println("</html>");

	}    
    
}

